package com.basicspring.myapp.filters;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class MeasurementInterceptors implements HandlerInterceptor {
	private static final Logger logger = LoggerFactory
			.getLogger(MeasurementInterceptors.class);

	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response,
			Object handler, ModelAndView modelAndView) throws Exception {
		long stopTime = System.currentTimeMillis();
		logger.info("Stop time "+stopTime);
		long timeDelta = stopTime - (Long)request.getAttribute("startTime");
		logger.error("Delta is "+timeDelta);
	}

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		long startTime = System.currentTimeMillis();
		request.setAttribute("startTime", startTime);
		logger.info("Start time "+startTime);
		return true;
	}

}
